package com.hzhr.iot.cqgas.ds.service;

import com.hzhr.iot.core.constant.CommonOptions;
import com.hzhr.iot.core.util.HzhrStringUtils;
import com.hzhr.iot.cqgas.ds.dao.smartmeter.example.EneWuziChangshangDao;
import com.hzhr.iot.cqgas.ds.dao.user.example.UsrCompanyDao;
import com.hzhr.iot.cqgas.ds.entity.smartmeter.EneWuziChangshangEntity;
import com.hzhr.iot.cqgas.ds.entity.user.UsrCompanyEntity;
import com.hzhr.iot.cqgas.ds.entity.user.UsrCompanyExample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CompanyService {
    @Autowired
    UsrCompanyDao usrCompanyDao;
    @Autowired
    EneWuziChangshangDao eneWuziChangshangDao;
    @Autowired
    UsrOptionsService usrOptionsService;

    private Logger logger = LoggerFactory.getLogger(CompanyService.class);

    public void syncIotCompany() {
        int addCount = 0;
        int updateCount = 0;
        long start = new Date().getTime();

        List<EneWuziChangshangEntity> eneWuziChangshangEntities = eneWuziChangshangDao.selectByExample(null);
        for (EneWuziChangshangEntity eneWuziChangshangEntity :
        eneWuziChangshangEntities) {
            try {
                String id = eneWuziChangshangEntity.getId();
                String des = eneWuziChangshangEntity.getDes();

                UsrCompanyExample usrCompanyExample = new UsrCompanyExample();
                usrCompanyExample.createCriteria().andOutIdEqualTo(id);
                List<UsrCompanyEntity> usrCompanyEntities = usrCompanyDao.selectByExample(usrCompanyExample);

                if (usrCompanyEntities.size() > 0) {
                    UsrCompanyEntity usrCompanyEntity = usrCompanyEntities.get(0);
                    usrCompanyEntity.setName(des);
                    usrCompanyEntity.setShortName(des);
                    usrCompanyEntity.setUpdateTime(new Date());
                    usrCompanyEntity.setUpdateUserId(0);
                    usrCompanyEntity.setIsValid(CommonOptions.YES_NO.YES);
                    usrCompanyEntity.setState("1");
                    usrCompanyDao.updateByPrimaryKey(usrCompanyEntity);
                    updateCount++;
                } else {
                    UsrCompanyEntity usrCompanyEntity = new UsrCompanyEntity();
                    usrCompanyEntity.setOutId(id);
                    usrCompanyEntity.setName(des);
                    usrCompanyEntity.setShortName(des);
                    usrCompanyEntity.setType(usrOptionsService.getCode("COMPANY_TYPE", "IOT_DEVICE_COMPANY"));
                    usrCompanyEntity.setIsAuth(CommonOptions.YES_NO.NO);
                    usrCompanyEntity.setCreateTime(new Date());
                    usrCompanyEntity.setCreateUserId(0);
                    usrCompanyEntity.setIsValid(CommonOptions.YES_NO.YES);
                    usrCompanyEntity.setState("1");
                    usrCompanyDao.insert(usrCompanyEntity);

                    //更新资源代码
                    usrCompanyEntity.setResourceCode("01" + HzhrStringUtils.fillZeroForLeft(usrCompanyEntity.getId().toString(), 4));
                    usrCompanyDao.updateByPrimaryKey(usrCompanyEntity);
                    addCount++;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        long end = new Date().getTime();
        logger.info("同步公司结束: " + (end - start) / 1000 + "s");
        logger.info("同步时间: " + (end - start) / 1000 + "s");
        logger.info("更新数量: " + updateCount + "条");
        logger.info("新增数量: " + addCount + "条");

    }
}
